package com.ruoyi.zhgd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.zhgd.domain.bo.ZjNWarningSwlqBo;
import com.ruoyi.zhgd.domain.vo.ZjNWarningSwlqVo;
import com.ruoyi.zhgd.domain.ZjNWarningSwlq;
import com.ruoyi.zhgd.mapper.ZjNWarningSwlqMapper;
import com.ruoyi.zhgd.service.IZjNWarningSwlqService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 水稳沥青拌合站预警信息Service业务层处理
 *
 * @author tll
 * @date 2024-02-29
 */
@RequiredArgsConstructor
@Service
@DS("sjcj")
public class ZjNWarningSwlqServiceImpl implements IZjNWarningSwlqService {

    private final ZjNWarningSwlqMapper baseMapper;

    /**
     * 查询水稳沥青拌合站预警信息
     */
    @Override
    public ZjNWarningSwlqVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询水稳沥青拌合站预警信息列表
     */
    @Override
    public TableDataInfo<ZjNWarningSwlqVo> queryPageList(ZjNWarningSwlqBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ZjNWarningSwlq> lqw = buildQueryWrapper(bo);
        Page<ZjNWarningSwlqVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询水稳沥青拌合站预警信息列表
     */
    @Override
    public List<ZjNWarningSwlqVo> queryList(ZjNWarningSwlqBo bo) {
        LambdaQueryWrapper<ZjNWarningSwlq> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ZjNWarningSwlq> buildQueryWrapper(ZjNWarningSwlqBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ZjNWarningSwlq> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getType()), ZjNWarningSwlq::getType, bo.getType());
        lqw.eq(bo.getInsterTime() != null, ZjNWarningSwlq::getInsterTime, bo.getInsterTime());
        lqw.eq(StringUtils.isNotBlank(bo.getBusinessId()), ZjNWarningSwlq::getBusinessId, bo.getBusinessId());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), ZjNWarningSwlq::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增水稳沥青拌合站预警信息
     */
    @Override
    public Boolean insertByBo(ZjNWarningSwlqBo bo) {
        ZjNWarningSwlq add = BeanUtil.toBean(bo, ZjNWarningSwlq.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改水稳沥青拌合站预警信息
     */
    @Override
    public Boolean updateByBo(ZjNWarningSwlqBo bo) {
        ZjNWarningSwlq update = BeanUtil.toBean(bo, ZjNWarningSwlq.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ZjNWarningSwlq entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除水稳沥青拌合站预警信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 查询水稳拌合站预警信息列表
     *
     * @param bo
     * @param pageQuery
     * @return
     */
    @Override
    public TableDataInfo<ZjNWarningSwlqVo> pageSw(ZjNWarningSwlqBo bo, PageQuery pageQuery) {
        Page<ZjNWarningSwlqVo> data = baseMapper.pageSw(bo, pageQuery.build());
        return TableDataInfo.build(data);
    }

    /**
     * 查询沥青拌合站预警信息列表
     *
     * @param bo
     * @param pageQuery
     * @return
     */
    @Override
    public TableDataInfo<ZjNWarningSwlqVo> pageLq(ZjNWarningSwlqBo bo, PageQuery pageQuery) {
        Page<ZjNWarningSwlqVo> data = baseMapper.pageLq(bo, pageQuery.build());
        return TableDataInfo.build(data);
    }
}
